window.onload=setTimeout("user_order()",1000) ;
function user_order() {
    var storage=window.localStorage;
    var user=storage.getItem("data");
    console.log(user);
    var user=JSON.parse(user);
    var username=user.user.userId;

    document.getElementById("username1").innerHTML=username;
    var data=username
    console.log("要发送的用户ID为："+data)
    var data=JSON.stringify(data)
    $.ajax({
        type: "get",
        url: "/order",
        contentType: "application/json;charset=UTF-8",
        dataType:"json",
        data: {
            "userId":data,
        },
        success: function (res) {
            if(res.code==2000){
                var data=res.data
                var order=data.order
                console.log("返回的数据为data："+data);
                console.log("返回的订单数据order为："+JSON.stringify(order));
                // setTimeout('location.assign("home.html")',2000);
                for(i=0;i<order.length;i++){
                    // var billDiv=document.getElementById("bill")
                    var time=order[i].createTime
                    var time=datetime(time);
                    var temp=order[i].status
                    var status=getStatus(temp)
                    var html="<tr><td>"+order[i].orderId+"</td><td>"+order[i].goodsName+"</td><td>"+
                        order[i].currentUnitPrice+"</td><td>"+order[i].quantity+"</td><td>"+
                        time+"</td><td>"+status+"</td></tr>"
                    $("#bill").append(html)
                    console.log("循环一次拼接成功"+i)
                }
            }
            else {
                console.log(res.code)
            }
        }
    })
}

//格式化时间日期，输出年月日时分秒
function datetime(da) {
    da = new Date(da);
    var year = da.getFullYear()
    var month = da.getMonth() + 1
    var date = da.getDate()
    var hour =da.getHours()
    var minute=da.getMinutes()
    var second=da.getSeconds()
    return [year,month,date,hour,minute,second].join('\:')
}
//根据返回的数值输出订单状态
function getStatus(status) {
    switch (status) {
        case 0:
            return "已取消"
            break
        case 10:
            return "未付款"
            break
        case 20:
            return "已付款"
            break
        case 40:
            return "已发货"
            break
        case 50:
            return "交易成功"
            break
        case 60:
            return "交易关闭"
            break
    }
}
// status：//订单状态:0-已取消-10-未付款，20-已付款，40-已发货，50-交易成功，60-交易关闭